package http

import (
	"resty.dev/v3"
	"time"
)

type Http interface {
	Get(url string)
}

type Client struct {
	engine *resty.Client
}

func NewClient() *Client {

	c := new(Client)

	engine := resty.New()
	engine.SetTimeout(3 * time.Second)

	//engine.AddRequestMiddleware(func(client *resty.Client, request *resty.Request) error {
	//	request.SetContext(context.WithValue(request.Context(), "startTime", time.Now()))
	//	return nil
	//})
	//
	//engine.AddResponseMiddleware(func(client *resty.Client, response *resty.Response) error {
	//	if !response.IsSuccess() {
	//		return nil
	//	}
	//
	//	startTime, ok := response.Request.Context().Value("startTime").(time.Time)
	//	if !ok {
	//		return nil
	//	}
	//
	//
	//	return nil
	//})

	c.engine = engine
	return c
}

func (client *Client) Get(url string) {
	req := client.engine.R()
	// 业内有一个广为流传的理念：“给用户一个明确的坏结果，远胜于让他无限期地等待。” 这正是超时控制在用户体验层面的核心体现。
	if tp99, ok := tp99Store.Load("url"); ok {
		timeout := tp99.(int64) * 3 / 2 // 增加50%的安全冗余
		req.SetTimeout(time.Duration(timeout) * time.Millisecond)
	}
	req.Get(url)
}

func int64ToFloat64(ints []int64) []float64 {
	floats := make([]float64, len(ints))
	for i, v := range ints {
		floats[i] = float64(v)
	}
	return floats
}
